package com.example.demo.batis;

import com.example.demo.control.UserBean;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@RestController
@RequestMapping("/user1")
public class User1Control {

    @Autowired
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    private User1Mapper user1Dao;
    @Autowired
    private SqlSessionUtils sessionUtils;

    @RequestMapping("/test1")
    public List<UserBean> selectAll() {
        return user1Dao.selectAll();
    }

    @RequestMapping("/test2")
    public UserBean selectById(int aa) {
        UserBean userBean = user1Dao.selectById(aa);
        System.out.println(userBean);
        if (userBean == null) {
            return new UserBean();
        }
        return userBean;
    }

    @RequestMapping("/test3")
    public String insert1(String name, String pd) {
        UserBean userBean = new UserBean();
        userBean.setName(name);
        userBean.setPd(pd);
        boolean b = user1Dao.insert(userBean);
        System.out.println("insert1 return " + b);
        System.out.println("insert1 id " + userBean.getId());
        return b ? "成功" : "失败";
    }

    @RequestMapping("/test4")
    public String insert2(String name, String pd) {
        UserBean userBean = new UserBean();
        userBean.setName(name);
        userBean.setPd(pd);
        /*在MyBatis中添加操作返回的是记录数并非记录主键id*/
        int b = user1Dao.insert2(userBean);
        System.out.println("insert2 return " + b);
        System.out.println("insert2 id " + userBean.getId());
        return b > 0 ? "成功" : "失败";
    }

    @RequestMapping("/test5")
    public String update1(int id, String pd) {
        UserBean userBean = new UserBean();
        userBean.setId(id);
        userBean.setPd(pd);
        int b = user1Dao.update2(userBean);
        System.out.println(b);
        return b > 0 ? "成功" : "失败";
    }

    /**
     * 原始的方式获取连接对象
     *
     * @param name
     * @param pd
     * @return
     */
    @RequestMapping("/test6")
    public String insert3(String name, String pd) {
        UserBean userBean = new UserBean();
        userBean.setName(name);
        userBean.setPd(pd);
        Integer rows = sessionUtils.getSession()
                .getMapper(User1Mapper.class)
                .insert2(userBean);
        System.out.println("insert3 rows = " + rows); // 添加操作返回记录数
        System.out.println("insert3 id = " + userBean.getId()); // 执行添加操作之后通过Java对象获取主键属性值
        return rows > 0 ? "成功" : "失败";
    }

    @RequestMapping("/test7")
    public List<UserBean> selectAll(String name, String pd) {
        UserBean userBean = new UserBean();
        userBean.setName(name);
        userBean.setPd(pd);
        Cursor<UserBean> rows = sessionUtils.getSession()
                .selectCursor(User1Mapper.class.getName() + ".selectAll");
        /*错误的此时也是要放map对象*/
//                .selectCursor("select * from user");
        List<UserBean> users = new ArrayList<>();
        rows.forEach(new Consumer<UserBean>() {
            @Override
            public void accept(UserBean user) {
                users.add(user);
            }
        });
        return users;
    }

    /**
     * 可直接获取ioc中的 SqlSessionFactory
     * @param name
     * @param pd
     * @return
     */
    @RequestMapping("/test8")
    public List<UserBean> selectAll2(String name, String pd) {
        UserBean userBean = new UserBean();
        userBean.setName(name);
        userBean.setPd(pd);
        List<UserBean> rows = sqlSessionFactory.openSession()
                .selectList(User1Mapper.class.getName()+".selectAll");
        return rows;
    }

    @Service
    public static class SqlSessionUtils {
        @Autowired
        DataSource dataSource;


        public SqlSession getSession() {
            TransactionFactory transactionFactory = new JdbcTransactionFactory();
            Environment environment = new Environment("development",
                    transactionFactory, dataSource);
            Configuration configuration = new Configuration(environment);
            configuration.addMapper(User1Mapper.class);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
            return sqlSessionFactory.openSession();
        }
    }

}
